//
//  CSGXInventoryManageController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/12/9.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXInventoryManageController.h"
#import "CSGXHomeSaleCategoryView.h"
#import "LKGXDropDownListButton.h"
#import "GXLinkViewController.h"
#import "CSGXInventoryManageFooter.h"
#import "CSGXInventoryManagerMasterController.h"

#import "TInventoryModel.h"
#import "TSaleInfoModel.h"
#import "TCommodityInfoModel.h"

#import "TInventoryDAL.h"
#import "TCommodityInfoDAL.h"

#import "CSGXInventoryManagerBLL.h"

@interface CSGXInventoryManageController () <CSGXCategoryViewActionDelegate, UIScrollViewDelegate>
    
    @property (nonatomic, weak) LKGXDropDownListButton *ddlInventoryMana;
    
    
    @property (nonatomic, weak) UIScrollView *svInventoryManager;
    
    @property (nonatomic, strong) GXLinkViewController *linkInventoryManaVc;
    
    @property (nonatomic, weak) CSGXInventoryManageFooter *inventoryManaFooter;
    
    
    
    @end

@implementation CSGXInventoryManageController
    
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadDataWithProductDateType:SelectWithProductDateNone];
    
#pragma mark call back drapDownListCallbackBlock
    self.ddlInventoryMana.drapDownListCallbackBlock = ^(NSInteger index) {
        //
        //        NSLog(@"%@", @(index).description);
        
        SelectWithProductDateType type = SelectWithProductDateNone;
        switch (index) {
            case 0:
            type = SelectWithProductDateNone;
            break;
            case 1:
            type = SelectWithProductDateTrue;
            break;
            
            default:
            break;
        }
        [self loadDataWithProductDateType:type];
    };
}
    
    
#pragma mark - Actions
- (void)inventoryManaFooterValueChanged:(CSGXInventoryManageFooter *)inventoryManaFooter {
    NSLog(@"inventoryManaFooter.isChecked--%zd", inventoryManaFooter.isChecked);
}
    
    
#pragma mark - methods
    
- (void)loadDataWithProductDateType:(SelectWithProductDateType)productDateType {
    /*
     今日出库 -销售数量
     可用库存 -库存数量
     
     1. 查询所有库存
     2. 根据商品号查询今日卖出（出库）
     
     */
    
    NSArray *tvPrefixHeaders = @[@"行", @"产品名称"];
    NSArray *tvContentHeaders = [NSArray array];
    
    NSMutableArray<NSArray *> *tvPrefixList = [NSMutableArray<NSArray *> array];
    NSMutableArray<NSArray *> *tvContentList = [NSMutableArray<NSArray *> array];
    
    //库存list
    NSArray<TInventoryModel *> *inventoryManagerList = [NSArray<TInventoryModel *> array];
    
    CGFloat totalMoney = 0;
    
#pragma mark 根据 “是否区分生产日期” 组织不同的数据
    //根据 “是否区分生产日期” 组织不同的数据
    if (productDateType == SelectWithProductDateNone) {
        //不区分
        tvContentHeaders = @[@"可用库存", @"昨日库存", @"今日入库", @"今日出库", @"预出库", @"调整数量", @"单价", @"金额"];
        inventoryManagerList = [TInventoryDAL loadListWithUserNo:[CSUserDefaultsTool getUserNo] isDiff:NO];
        self.linkInventoryManaVc.isNeedCreate = NO;
        
    }else if (productDateType == SelectWithProductDateTrue) {
        //区分
        self.linkInventoryManaVc.isNeedCreate = YES;
        tvContentHeaders = @[@"生产日期", @"可用库存", @"昨日库存", @"今日入库", @"今日出库", @"预出库", @"调整数量", @"单价", @"金额"];
        inventoryManagerList = [TInventoryDAL loadListWithUserNo:[CSUserDefaultsTool getUserNo] isDiff:YES];
        
    }
    if (inventoryManagerList == nil || inventoryManagerList.count == 0) {
        return;
    }
    
    //根据是否区分生产日期查询到数据之后，处理list。
    for (int i = 0; i < inventoryManagerList.count; i++) {
        
        NSArray<TSaleInfoModel *> *invManaSaleList = [CSGXInventoryManagerBLL loadSaleListWithCommNO:inventoryManagerList[i].iCoNo
                                                                                             timeDay:[CSGXDateFormatterManager sharedManager].currentDay];
        
        NSString *saleCountToday = @"0,0,0";
        if (invManaSaleList && invManaSaleList.count >0 ) {
            saleCountToday = [CSGXInventoryManagerBLL getSaleCountTodayWithSaleList:invManaSaleList];
        }
        //可用库存
        NSString *inventoryStr = [CSGXInventoryManagerBLL getThreeUnitStr:inventoryManagerList[i].iUnit countStr:inventoryManagerList[i].iInventoryCount];
        //单价
        TCommodityInfoModel *commMo = [TCommodityInfoDAL loadModelWithCommNo:inventoryManagerList[i].iCoNo];
        NSString *priceStr = [CSGXInventoryManagerBLL getThreeUnitStr:inventoryManagerList[i].iUnit countStr:commMo.cPrice];
        //当前库存总价
        CGFloat totalInventoryMoney = [CSGXInventoryManagerBLL getTotalMoneyWithPriceStr:priceStr count:inventoryStr];
        totalMoney += totalInventoryMoney;
        //
        [tvPrefixList addObject:@[@(i + 1), inventoryManagerList[i].iName]];
        
        if (productDateType == SelectWithProductDateNone) {
            //不区分
            [tvContentList addObject:@[
                                       inventoryStr, //可用库存
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]], //昨日库存
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]], //今日入库
                                       [CSGXInventoryManagerBLL getThreeUnitStr:inventoryManagerList[i].iUnit countStr:saleCountToday], //今日出库(销售数量)
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]],  //预出库
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]],  //调整数量
                                       priceStr,//单价
                                       [NSString stringWithFormat:@"%.2f", totalInventoryMoney] //金额(一条本库存中的总金额, 数量*单价)
                                       ]];

        }else if (productDateType == SelectWithProductDateTrue) {
            //区分
            [tvContentList addObject:@[
                                       [inventoryManagerList[i].iCreateTime subStringFrontOfKey:@" "], //生产日期
                                       inventoryStr, //可用库存
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]], //昨日库存
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]], //今日入库
                                       [CSGXInventoryManagerBLL getThreeUnitStr:inventoryManagerList[i].iUnit countStr:saleCountToday], //今日出库(销售数量)
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]],  //预出库
                                       [NSString stringWithFormat:@",,0%@", [inventoryManagerList[i].iUnit subStringBackOfLastIndex:1]],  //调整数量
                                       priceStr,//单价
                                       [NSString stringWithFormat:@"%.2f", totalInventoryMoney] //金额(一条本库存中的总金额, 数量*单价)
                                       ]];

        }
        
    }
    
    self.linkInventoryManaVc.linkDict = @{@"tvPrefixHeaders": tvPrefixHeaders,
                                          @"tvPrefixList": tvPrefixList,
                                          @"tvContentHeaders": tvContentHeaders,
                                          @"tvContentList": tvContentList
                                          };
    
    self.inventoryManaFooter.inventoryManagerFooterList = inventoryManagerList;
    self.inventoryManaFooter.totalInventoryMoney = totalMoney;
    
    
    //    //
    //    self.linkInventoryManaVc.linkDict = @{@"tvPrefixHeaders":@[@"行", @"产品名称"],
    //                                      @"tvPrefixList":@[
    //                                              @[@"AA",  @"BBBBBBBBBBBBBBBB"],
    //                                              @[@"AA",    @"BBBBBBBBBBBBBBBB"],
    //                                              @[@"AA",    @"BBBBBBBBBBBBBBBB"]],
    //                                      @"tvContentHeaders":@[@"可用库存", @"昨日库存", @"今日入库", @"今日出库", @"预出库", @"调整数量", @"单价", @"金额"],
    //                                      @"tvContentList":@[
    //                                              @[@"001,000,100",      @"002,,", @",,llllllll", @",4000,", @",5000,", @",,6000", @"70,,00",       @"8000"],
    //                                              @[@"001,000100,03001", @"002,,", @",,llllllll", @",,4000", @",,5000", @"6000,,", @"7000,,8888",   @"8000"],
    //                                              @[@"001,000100,04001", @",002,", @",,llllllll", @"4000,,", @",,5000", @"6000,,", @"7,,000",       @"8000"]
    //                                              ]};
    
}
    
    
    
    
    
#pragma mark - delegates
- (void)gxCategoryView:(CSGXHomeSaleCategoryView *)categoryView withSelectedIndex:(NSInteger)selectedIdnex {
    if (selectedIdnex == 0) {
        self.svInventoryManager.contentOffset = CGPointMake(0, 0);
    }else {
        self.svInventoryManager.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    }
}
    
    
    
    
#pragma mark - setupUI
- (void)setupUI {
    CGFloat invManaFooterHeight = 30;
    //
    CSGXHomeSaleCategoryView *vSegment = [CSGXHomeSaleCategoryView gxCategoryViewWithDelegate:self page:-1];
    [self.view addSubview:vSegment];
    
    [vSegment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view).mas_offset(0);
        make.height.mas_offset(25);
    }];
    
    vSegment.segments = @[@"车库库存", @"主库存"];
    
    //svInventoryManager
    UIScrollView *svInventoryManager = [[UIScrollView alloc] init];
    [self.view addSubview:svInventoryManager];
    self.svInventoryManager = svInventoryManager;
    
    [svInventoryManager mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vSegment.mas_bottom).mas_offset(0);
        make.leading.bottom.trailing.equalTo(self.view).mas_offset(0);
    }];
    [self.view layoutIfNeeded];
    
    svInventoryManager.bounces = NO;
    svInventoryManager.showsVerticalScrollIndicator = NO;
    svInventoryManager.showsHorizontalScrollIndicator = NO;
    svInventoryManager.pagingEnabled = YES;
    svInventoryManager.scrollEnabled = NO;
    
    UIView *vSize = [[UIView alloc] init];
    [svInventoryManager addSubview:vSize];
    
    [vSize mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(svInventoryManager);
        make.width.mas_offset(2 * [UIScreen mainScreen].bounds.size.width);
        make.height.equalTo(svInventoryManager).mas_offset(0);
    }];
    
    //  ddlInventoryMana
    LKGXDropDownListButton *ddlInventoryMana = [LKGXDropDownListButton gx_dropDownListWithTextList:@[@"不区分生产日期", @"区分生产日期"] dictionary:@{@"fontSize":@(13), @"normalColor":[UIColor blackColor]}];
    [vSize addSubview:ddlInventoryMana];
    self.ddlInventoryMana = ddlInventoryMana;
    
    [ddlInventoryMana mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(vSize).mas_offset(0);
        make.height.mas_offset(35);
        make.width.equalTo(vSize).multipliedBy(0.5);
    }];
    [self.view layoutIfNeeded];
    
    //-vLink
    UIView *vLink = [[UIView alloc] init];
    [vSize addSubview:vLink];
    
    [vLink mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ddlInventoryMana.mas_bottom).mas_offset(0);
        make.leading.equalTo(vSize).mas_offset(0);
        make.width.equalTo(vSize).multipliedBy(0.5);
        make.height.equalTo(vSize).mas_offset(-invManaFooterHeight);
    }];
    [self.view layoutIfNeeded];
    
    //-vMaster
    UIView *vMaster = [[UIView alloc] init];
    [vSize addSubview:vMaster];
    
    //    vMaster.backgroundColor = [UIColor redColor];
    
    [vMaster mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(vSize).mas_offset(0);
        make.leading.equalTo(vLink.mas_trailing).mas_offset(0);
    }];
    [self.view layoutIfNeeded];
    
    //GXLinkViewController
    GXLinkViewController *linkVc = [[GXLinkViewController alloc] init];
    linkVc.viewcontrollerEnum = ViewControllerInventoryManager;
    [self gx_addChildController:linkVc intoView:vLink];
    self.linkInventoryManaVc = linkVc;
    
    [linkVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(vLink).mas_offset(0);
        make.bottom.equalTo(vLink).mas_offset(0);
    }];
    
    
    //footer
    CSGXInventoryManageFooter *inventoryManaFooter = [[CSGXInventoryManageFooter alloc] init];
    [vSize addSubview:inventoryManaFooter];
    self.inventoryManaFooter = inventoryManaFooter;
    
    inventoryManaFooter.backgroundColor = kColorBar;
    [inventoryManaFooter addTarget:self action:@selector(inventoryManaFooterValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    [inventoryManaFooter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.equalTo(vSize).mas_offset(0);
        make.height.mas_offset(invManaFooterHeight);
        make.width.equalTo(vLink);
    }];
    [self.view layoutIfNeeded];
    
    //CSGXInventoryManagerMasterController
    CSGXInventoryManagerMasterController *masterVc = [[CSGXInventoryManagerMasterController alloc] init];
    
    [self gx_addChildController:masterVc intoView:vMaster];
    
    [masterVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(vMaster).mas_offset(0);
    }];
    
}
    
- (void)btn1Click:(id)sender {
    NSLog(@"btnClick");
}
    
    
    
    
    
    
    
    
    
    
    
    
@end
